package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.GetMoneyReport;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.ReportDao;
import com.itheima.entity.Result;
import com.itheima.reportPojo.ReportEveryReturn;
import com.itheima.service.ReportService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 运营数据统计服务
 */
@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService {
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private ReportDao reportDao;
    //查询运营数据
    public Map<String, Object> getBusinessReportData() throws Exception{
        //报表日期
        String today = DateUtils.parseDate2String(DateUtils.getToday());
        //获得本周一日期
        String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        //获得本月第一天日期
        String firstDay4ThisMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());

        //本日新增会员数
        Integer todayNewMember = memberDao.findMemberCountByDate(today);
        //总会员数
        Integer totalMember = memberDao.findMemberTotalCount();
        //本周新增会员数
        Integer thisWeekNewMember = memberDao.findMemberCountAfterDate(thisWeekMonday);
        //本月新增会员数
        Integer thisMonthNewMember = memberDao.findMemberCountAfterDate(firstDay4ThisMonth);

        //今日预约数
        Integer todayOrderNumber = orderDao.findOrderCountByDate(today);
        //本周预约数
        Integer thisWeekOrderNumber = orderDao.findOrderCountAfterDate(thisWeekMonday);
        //本月预约数
        Integer thisMonthOrderNumber = orderDao.findOrderCountAfterDate(firstDay4ThisMonth);
        //今日到诊数
        Integer todayVisitsNumber = orderDao.findVisitsCountByDate(today);
        //本周到诊数
        Integer thisWeekVisitsNumber = orderDao.findVisitsCountAfterDate(thisWeekMonday);
        //本月到诊数
        Integer thisMonthVisitsNumber = orderDao.findVisitsCountAfterDate(firstDay4ThisMonth);

        //热门套餐查询
        List<Map> hotSetmeal = orderDao.findHotSetmeal();

        Map<String,Object> result = new HashMap<>();
        result.put("reportDate",today);
        result.put("todayNewMember",todayNewMember);
        result.put("totalMember",totalMember);
        result.put("thisWeekNewMember",thisWeekNewMember);
        result.put("thisMonthNewMember",thisMonthNewMember);
        result.put("todayOrderNumber",todayOrderNumber);
        result.put("thisWeekOrderNumber",thisWeekOrderNumber);
        result.put("thisMonthOrderNumber",thisMonthOrderNumber);
        result.put("todayVisitsNumber",todayVisitsNumber);
        result.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
        result.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
        result.put("hotSetmeal",hotSetmeal);
        return result;
    }

    //
    @Override
    public Integer findOrderCountByDate(String today) {
        return orderDao.findOrderCountByDate(today);
    }
    //
    @Override
    public Integer findVisitsCountByDate(String today) {
        return orderDao.findVisitsCountByDate(today);
    }
    //收入方法
    @Override
    public ArrayList<GetMoneyReport> getMoney(String oneDate, String twoDate) {
       return memberDao.getMoney(oneDate,twoDate);

    }

    @Override
    public Map OrderAndDiagnosis(String value) {
        //创建一个map,每次根据一个日期查询到两个数值,分别对应key lineOne 和key lineTwo中
        Map<String, Object> map =   new HashMap<>();
        //查询到的日期集合
        ArrayList<String> dateDay  = new ArrayList<>();
        //查询到的预约集合
        ArrayList<Integer> lineOrder  = new ArrayList<>();
        //查询到的已到诊集合
        ArrayList<Integer> lineDiagnosis = new ArrayList<>();
        //查询到的日期集合
        ArrayList<String> dataDay = new ArrayList<>();
        //前台来的数据["2020-09-01","2020-09-12"];
        //使用截取拆分,获得两个日期
        String firstDate = value.substring(2, 12);
        String lastDate = value.substring(15, 25);
         //根据日期日期区间,返回一个集合.里面记录每一天预约人数
        List<ReportEveryReturn> oder = reportDao.findOder(firstDate, lastDate);
        List<ReportEveryReturn> diagnosis = reportDao.findDiagnosis(firstDate, lastDate);
        //根据日期日期区间,返回一个集合.里面记录每一天的已到诊人数
        for (ReportEveryReturn reportEveryReturn1 : oder) {
            //日期封装完毕
            dateDay.add(reportEveryReturn1.getOrderDate());
            //预约数量封装完毕
            lineOrder.add(reportEveryReturn1.getAmount());
            //遍历已到诊集合

            for (ReportEveryReturn reportEveryReturn2 : diagnosis) {
                if(!reportEveryReturn1.getOrderDate().equalsIgnoreCase(reportEveryReturn2.getOrderDate())){
                    lineDiagnosis.add(0);
                    break;
                }else{
                    lineDiagnosis.add(reportEveryReturn2.getAmount());
                    break;
                }

            }

        }
        map.put("dateDay",dateDay);
        map.put("lineOrder",lineOrder);
        map.put("lineDiagnosis",lineDiagnosis);




      return map;

    }
    public static String UTCStringtODefaultString(String UTCString) {
        try {
            if (true) {
                UTCString = UTCString.replace("Z", " UTC");
                SimpleDateFormat utcFormat = new SimpleDateFormat("yyyy-MM-dd");
                SimpleDateFormat defaultFormat = new SimpleDateFormat("yyyy-MM-dd");
                Date date = utcFormat.parse(UTCString);
                return defaultFormat.format(date);
            } else {
                return null;
            }

        } catch (ParseException pe) {
            pe.printStackTrace();
            return null;
        }
    }

}
